package com.zichan360.common;

/**
 * 创建用户:杨辽
 * 创建时间: 2018-03-14 14:41
 * 描   述:
 **/
public class BaseResponse<T> {

    private boolean status;

    private String message;

    private int errorCode;

    private Object result;

    private T page;

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public T getPage() {
        return page;
    }

    public void setPage(T page) {
        this.page = page;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static <T> BaseResponse buildResponse(Object result) {
        return buildResponse(true, "成功", result);
    }

    /**
     * 建立通用返回体
     *
     * @param isSuccess 是否成功
     * @param message   返回信息
     * @param result    消息主体
     * @return
     * @author mayixin
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static <T> BaseResponse buildResponse(Boolean isSuccess, String message, Object result) {
        BaseResponse baseResponse;
        if (isSuccess) {
            baseResponse = getSuccessRes();
            if (message == null || "".equals(message.trim())) {
                baseResponse.setMessage("false");
            } else {
                baseResponse.setMessage(message);
            }
            baseResponse.setResult(result);
            return baseResponse;
        } else {
            baseResponse = getFailedRes(message);
            return baseResponse;
        }

    }


    private static BaseResponse getSuccessRes() {
        return new BaseResponse(true, "", 0);
    }

    private static BaseResponse getFailedRes(String message) {
        return new BaseResponse(false, message, 0);
    }


    protected BaseResponse(boolean status, String message, int errorCode) {
        this.status = status;
        this.message = message;
        this.errorCode = errorCode;
        this.result = result;
        this.page = page;
    }
}
